package com.twu.biblioteca;

import java.util.Arrays;
import java.util.List;

public abstract class Collection {
    private List<Item> items;

    Collection(Item... items) {
        this.items = Arrays.asList(items);
    }

    public boolean checkOutItem(String bookName) {
        for (Item item : items) {
            if (item.name.compareToIgnoreCase(bookName) == 0) {
                return item.checkOutItem();
            }
        }
        return false;
    }

    public boolean returnItem(String bookName) {
        for (Item item : items) {
            if (item.name.compareToIgnoreCase(bookName) == 0) {
                return item.returnItem();
            }
        }
        return false;
    }

    public String toFormattedString() {
        StringBuilder ret = new StringBuilder();
        StringBuilder bookOutputString = new StringBuilder();
        ret.append(String.format(format(), titles()));
        for (Item item : items) {
            bookOutputString.append(item.toFormattedString(format()));
        }
        if (bookOutputString.length() == 0) {
            return "All items are checked out!";
        } else {
            return ret.append(bookOutputString).toString();
        }
    }

    protected abstract String[] titles();

    protected abstract String format();


}
